package com.dreamoo.common.threadpool;

import com.dreamoo.common.threadpool.wrapper.DreamooTaskWrapper;
import jakarta.validation.constraints.NotNull;

import java.util.concurrent.*;

/**
 * @author 山顶洞人郑某
 * @className CompletableFutureExecutor.java 用于传入CompletableFuture的线程池
 * @date 2025 05 16
 */
public class CompletableFutureExecutor extends ThreadPoolExecutor {

    /**
     * Creates a new {@code ThreadPoolExecutor} with the given initial
     * parameters, the
     *
     * and the {@linkplain AbortPolicy
     * default rejected execution handler}.
     *
     * <p>It may be more convenient to use one of the {@link Executors}
     * factory methods instead of this general purpose constructor.
     *
     * @param corePoolSize    the number of threads to keep in the pool, even
     *                        if they are idle, unless {@code allowCoreThreadTimeOut} is set
     * @param maximumPoolSize the maximum number of threads to allow in the
     *                        pool
     * @param keepAliveTime   when the number of threads is greater than
     *                        the core, this is the maximum time that excess idle threads
     *                        will wait for new tasks before terminating.
     * @param unit            the time unit for the {@code keepAliveTime} argument
     * @param workQueue       the queue to use for holding tasks before they are
     *                        executed.  This queue will hold only the {@code Runnable}
     *                        tasks submitted by the {@code execute} method.
     * @throws IllegalArgumentException if one of the following holds:<br>
     *                                  {@code corePoolSize < 0}<br>
     *                                  {@code keepAliveTime < 0}<br>
     *                                  {@code maximumPoolSize <= 0}<br>
     *                                  {@code maximumPoolSize < corePoolSize}
     * @throws NullPointerException     if {@code workQueue} is null
     */
    public CompletableFutureExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    public CompletableFutureExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit timeUnit, LinkedBlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, CallerRunsPolicy callerRunsPolicy) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, timeUnit, workQueue, threadFactory, callerRunsPolicy);
    }

    @Override
    public void execute(@NotNull Runnable command) {
        if (command == null) {
            throw new NullPointerException("任务为空");
        }
        super.execute(DreamooTaskWrapper.wrap(command));
    }

    @Override
    public <T> Future<T> submit(@NotNull Callable<T> task) {
        if (task == null) {
            throw new NullPointerException("任务为空");
        }
        return super.submit(DreamooTaskWrapper.wrap(task));
    }
}
